from gpiozero import RGBLED
from gpiozero.pins.pigpio import PiGPIOFactory
from colorzero import Color
import time

class Car_Leds:
   FORWARD = 0
   BACKWARD = 1
   STOP = 2
   LEFT = 3
   RIGHT = 4
   LEFT_STEP = 5
   RIGHT_STEP = 6
   LEFT_HEAD=7  
   RIGHT_HEAD=8
   GO=9
   
   LEDS_DANCING_OFF=0
   LEDS_DANCING_ON=1
    
   #def __init__(self,chip,Right_Red,Right_Green,Right_Blue,Left_Red=None,Leftt_Green=None,Left_Blue=None):
   #   self.Right_Led=Rgb_Led(chip,Right_Red,Right_Green,Right_Blue)
   #   self.Left_Led=Rgb_Led(chip,Left_Red,Leftt_Green,Left_Blue)
    
   def __init__(self,Right_Red,Right_Green,Right_Blue,Left_Red,Left_Green,Left_Blue):
      self.Right_Led=RGBLED(red=Right_Red,green=Right_Green,blue=Right_Blue, pwm = True,active_high=True,initial_value=(0,0, 1),pin_factory= PiGPIOFactory())
      self.Left_Led=RGBLED(red=Left_Red,green=Left_Green,blue=Left_Blue, pwm = True,active_high= True,initial_value=(0, 0, 1),pin_factory= PiGPIOFactory())
    
        
      self.DICT_CMD_ACTION = {Car_Leds.FORWARD: self.forward,
                                Car_Leds.BACKWARD: self.backward,
                                Car_Leds.STOP: self.stop,
                                Car_Leds.LEFT: self.turn_left,
                                Car_Leds.RIGHT: self.turn_right,
                                Car_Leds.LEFT_STEP: self.stop,
                                Car_Leds.RIGHT_STEP: self.stop,
                                Car_Leds.LEFT_HEAD: self.left_head,
                                Car_Leds.RIGHT_HEAD: self.right_head,
                                Car_Leds.GO: self.forward
                                }
      self.dancing = False
      self.car_status= Car_Leds.STOP
    
   def forward(self):
      self.Right_Led.color=Color("green")
      self.Left_Led.color=Color("green")

   def backward(self):
      self.Right_Led.color=Color("yellow")
      self.Left_Led.color=Color("yellow")    
    
   def turn_left(self):
      self.Right_Led.off()
      self.Left_Led.color=Color("green")

   def turn_right(self):
      self.Right_Led.color=Color("green")
      self.Left_Led.off()
         
   def stop(self):
      self.Right_Led.color=Color("red")
      self.Left_Led.color=Color("red")  

   def left_head(self):
       self.Right_Led.color=Color("blue")
       self.Left_Led.color=Color("green")  
   
   def right_head(self):
       self.Right_Led.color=Color("green")
       self.Left_Led.color=Color("blue")  
  
   def action(self, cmd_to_motor):
      self.dancing=False
      speed_cmds=[101,102,201,202,301,302]
      if (cmd_to_motor not in speed_cmds):
         self.car_status= cmd_to_motor
         self.DICT_CMD_ACTION[cmd_to_motor]()    
   
   def dancing_action(self, cmd):
      if (cmd == Car_Leds.LEDS_DANCING_ON):
         self.dancing = True
      elif (cmd == Car_Leds.LEDS_DANCING_OFF):
         self.dancing = False
   
   def fading(self):
      #for color in ["red","green","blue"]:
      #   self.Right_Led.blink(fade_in_time=3, fade_out_time=3,on_color=Color(1,1,1), off_color=Color(color))
      #   self.Left_Led.blink(fade_in_time=3, fade_out_time=3,on_color=Color(1,1,1), off_color=Color(color))
      for color in ["red","green","blue"]:
         for duty_cycle in range(100):
            if (color=="red"):
               self.Right_Led.color =(duty_cycle/100,0,0)
               self.Left_Led.color =(duty_cycle/100,0,0)
            elif (color=="green"):
               self.Right_Led.color =(0,duty_cycle/100,0)
               self.Left_Led.color =(0,duty_cycle/100,0)
            elif (color=="blue"):
               self.Right_Led.color =(0,0,duty_cycle/100)
               self.Left_Led.color =(0,0,duty_cycle/100)
            time.sleep(0.05) 
    
   def leds_dancing(self):
      while True:
         if self.dancing:
            self.forward()
            if self.dancing:
               time.sleep(0.6)
            else:
              continue   
            self.stop()
            if self.dancing:
               time.sleep(0.6)
            else:
               continue   
            self.backward()       
            if self.dancing:
               time.sleep(0.6)
            else:
               continue  
            self.left_head()    
            if self.dancing:
               time.sleep(0.6)
            else:
              continue   
            self.right_head()    
            if self.dancing:
               time.sleep(0.6)
            else:
              continue   
            self.Right_Led.color=Color("yellow")
            self.Left_Led.color=Color("yellow")         
            if self.dancing:
               time.sleep(0.6)
            else:
              continue   
            self.Right_Led.color=Color("aqua")   
            self.Left_Led.color=Color("aqua")   
            if self.dancing:
               time.sleep(0.6)
            else:
               continue   
            self.Right_Led.color = Color('magenta')
            self.Left_Led.color = Color('magenta')
            if self.dancing:
               time.sleep(0.6)
            else:
               continue   
            self.Right_Led.color = Color('white')
            self.Left_Led.color = Color('white')
            if self.dancing:
               time.sleep(0.6)
            else:
               continue   
            self.fading()
            if self.dancing:
               time.sleep(0.6)
            else:
               continue    
         else: 
            self.action(self.car_status)
         time.sleep(0.1)
    
if __name__ == '__main__': 
    import time
    leds =  Car_Leds(16,20,21,23,24,25)
    #time.sleep(2)
    #leds.forward()
    #time.sleep(2)
    #leds.back()
    #time.sleep(2)
    #leds.stop()
    #time.sleep(2)
   # leds.Right_Led.blink(on_time=2, off_time=2, fade_in_time=0, fade_out_time=0,on_color=Color('red'), off_color=Color('blue'),n=10)
   # leds.Left_Led.blink(on_time=2, off_time=2, fade_in_time=0, fade_out_time=0,on_color=Color('red'), off_color=Color('blue'),n=10)
    leds.fading()
    time.sleep(3)
    #leds.dancing=True
    #leds.leds_dancing()
    
 